package org.hyy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hyy.dao.CategoryDAO;
import org.hyy.vo.Category;

import com.opensymphony.xwork2.ActionSupport;

public class BasicAction extends ActionSupport implements SessionAware , CookiesAware{
	private static final long serialVersionUID = -7649755461857563144L;
    @SuppressWarnings("rawtypes")
	protected Map session;
    protected Map<String, String> cookie;
    protected String user;
    protected String basePath;
    private List<Category> allcategory;
	
    public String getBasePath() {
    	HttpServletRequest request = getRequest();
    	basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    	return basePath;
	}
    
	public void setCookiesMap(Map<String, String> cookies) {
		this.cookie = cookies;
	}
	
    @SuppressWarnings("rawtypes")
	public void setSession(Map session) {
        this.session = session;
    }
	
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}
	
	public String getUser(){
		user = (String)session.get("user");
    	return user;
    }
	
	protected String out(Object obj) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.print(obj);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Category> getAllcategory() {
		//声明
		CategoryDAO categoryDAO = new CategoryDAO();
		allcategory = new ArrayList<Category>();
		allcategory = categoryDAO.GetCatergories();
		return allcategory;
	}
}
